package com.dengqi.mymodule.activity.guide;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.dengqi.mymodule.BaseActivity;
import com.dengqi.mymodule.MainActivity;
import com.dengqi.mymodule.R;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;

/**
 * 坑一：有可能不带状态栏有可能带状态栏
 * 坑二：最后一页有按钮。a.放布局内显示隐藏来控制。b.自定义第三页的View加上按钮
 * 坑三：点点点做在图片上，或者自己实现
 * Created by dell on 2018/6/19
 */

public class ViewPagerActivity extends BaseActivity {

    Button btn_enter;
    ViewPager mViewPager;
    ViewPagerAdatper mAdapter;
    List<View> imgLists = new ArrayList<>();
    int[] imgs = new int[]{R.mipmap.bg_1, R.mipmap.bg_2, R.mipmap.bg_3};
    //            , R.mipmap.bg_4, R.mipmap.bg_5, R.mipmap.bg_6, R.mipmap.bg_7, R.mipmap.bg_8, R.mipmap.bg_9, R.mipmap.bg_10};

    LinearLayout in_ll;
    ImageView iv_light_dots;
    int mDistance = 0;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide_viewpager);


        fullscreen();
        initViewPager();
        initDotView();

    }

    /**
     * 全屏
     */
    private void fullscreen(){
        /**
         * 隐藏状态栏和部分手机导航栏
         */
//        getWindow().getDecorView().setSystemUiVisibility(
//        View.SYSTEM_UI_FLAG_LAYOUT_STABLE
//                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
//                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION//隐藏nav栏
//                | View.SYSTEM_UI_FLAG_FULLSCREEN//隐藏状态栏
//                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);

    }


    /**
     * viewpager与adapter
     */
    private void initViewPager(){
        btn_enter = findViewById(R.id.btn_enter);
        mViewPager = findViewById(R.id.viewPager);

        for (int i = 0; i < imgs.length; i++) {          // 添加图片源
            ImageView iv = new ImageView(this);
            iv.setImageResource(imgs[i]);
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            imgLists.add(iv);

        }

        mAdapter = new ViewPagerAdatper(imgLists);
        mViewPager.setAdapter(mAdapter);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                //页面滚动时小白点移动的距离，并通过setLayoutParams(params)不断更新其位置
                float leftMargin = mDistance * (position + positionOffset);
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) iv_light_dots.getLayoutParams();
                params.leftMargin = (int) leftMargin;
                iv_light_dots.setLayoutParams(params);
            }

            @Override
            public void onPageSelected(int position) {
                if (position == imgs.length - 1) {
                    btn_enter.setVisibility(View.VISIBLE);
                } else {
                    btn_enter.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }



    /**
     * 点布局
     */
    private void initDotView(){
        in_ll = findViewById(R.id.in_ll);
        iv_light_dots = findViewById(R.id.iv_light_dots);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(0, 0, 40, 0);
        for (int i = 0; i < imgs.length; i++) {          // 添加图片源
            ImageView iv = new ImageView(this);
            iv.setImageResource(R.drawable.gray_dot);
            if (i != imgs.length - 1) {
                in_ll.addView(iv, layoutParams);
            } else {
                in_ll.addView(iv);
            }

        }
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        /**
         * 计算点之间的距离
         */
        if (in_ll.getChildCount() > 2) {
            mDistance = in_ll.getChildAt(1).getLeft() - in_ll.getChildAt(0).getLeft();
        }

    }

    public void toMainActivity() {
        startActivity(new Intent(ViewPagerActivity.this, MainActivity.class));
        overridePendingTransition(0, 0);
    }

    public void onBtn1(View view) {
        toMainActivity();
    }

}
